package 链表;

public class 合并两个有序链表21 {
  public class ListNode {
     int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
      ListNode dummy = new ListNode();
      ListNode p = dummy;
      while (list1!=null&&list2!=null){
          if(list1.val>list2.val){
              p.next = list2;
              list2 = list2.next;
          }else{
              p.next = list1;
              list1 =list1.next;
          }
          p=p.next;
      }
      if(list1!=null){
          p.next=list1;
      }else {
          p.next=list2;
      }
      return dummy.next;
    }
    }
